<?php

namespace App\Traits\Relation;

use App\Models\City;
use App\Models\District;
use App\Models\ExpressCompany;
use App\Models\NetworkAdmin;
use App\Models\Province;
use App\Models\Service;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * 用于定义网点和其他模型的关系
 *
 * @package App\Traits\Relation
 * Date: 2018/06/11
 * @author Tenny
 */
trait NetworkRelation
{
    /**
     * 获取关联站点数
     *
     * Date: 2018/06/11
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function services()
    {
        return $this->belongsToMany(Service::class, 'network_express', 'network_id', 'service_id', 'id', 'id')->withPivot('credit', 'unit_price');
    }

    /**
     * 获取关联省份
     *
     * Date: 2018/06/11
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function province()
    {
        return $this->HasOne(Province::class, 'id', 'province_id');
    }

    /**
     * 获取关联城市
     *
     * Date: 2018/06/11
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function city()
    {
        return $this->HasOne(City::class, 'id', 'city_id');
    }

    /**
     * 获取关联县\区
     *
     * Date: 2018/06/11
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function district()
    {
        return $this->HasOne(District::class, 'id', 'district_id');
    }

    /**
     * 获取关联快递公司
     *
     * Date: 2018/06/11
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function express()
    {
        return $this->HasOne(ExpressCompany::class, 'id', 'express_id');
    }

    /**
     * 获取关联网点管理员
     *
     * Date: 2018/06/11
     * @author Tenny
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function admins()
    {
        return $this->HasOne(NetworkAdmin::class, 'network_id', 'id');
    }
}
